package com.leyou.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.search.pojo.SkuDTO;
import com.leyou.search.pojo.SpuDTO;
import com.leyou.search.pojo.SpuDetailDTO;
import com.leyou.service.IgoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {
    @Autowired
    private IgoodsService goodsService;

    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuDTO>> querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "key", required = false) String key
    ) {
        return ResponseEntity.ok(this.goodsService.querySpuByPage(page, rows, saleable, key));

    }

    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO) {
        this.goodsService.saveGoods(spuDTO);
        return ResponseEntity.ok().build();
    }

    @PutMapping("spu/saleable")
    public ResponseEntity<Void> updateSpuSaleable(@RequestParam("id") Long id, @RequestParam("saleable") Boolean saleable) {
        goodsService.updateSpuSaleable(id, saleable);
        return ResponseEntity.ok().build();
    }

    @GetMapping("spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long spuId) {
        return ResponseEntity.ok(goodsService.querySpuDetailById(spuId));

    }

    @GetMapping("sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long spuId) {
        return ResponseEntity.ok(goodsService.querySkuBySpuId(spuId));

    }

    @PutMapping("goods")
    public ResponseEntity<Void> UpdateGoods(@RequestBody SpuDTO spuDTO) {
        this.goodsService.UpdateGoods(spuDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据spu的id查询spu
     *
     * @param id
     * @return
     */
    @GetMapping("spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(goodsService.querySpuById(id));
    }

    /**
     * 购物车校验商品状态
     * @param ids
     * @return
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids")List<Long> ids){
        return ResponseEntity.ok(goodsService.querySkuByIds(ids));
    }

    @PutMapping("stock/minus")
    public ResponseEntity<Void> minusStock(@RequestBody Map<Long, Integer> map){
        goodsService.minusStock(map);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PutMapping("/stock/plus")
    public ResponseEntity<Void> plusStock(@RequestBody Map<Long, Integer> map){
        goodsService.plusStock(map);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
